﻿// =============================================================================
// XPathDebugger And Examples StringUtils.cs
// Copyright (c) 2013 TNTTechnologies Inc.
// =============================================================================

#region

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace Utilities{
    internal class Util{
        /// <summary>
        ///     Note: not high performance.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static string[] Replace(string[] source, string from, string to){
            List<string> sourceList = source.ToList();

            for (int i = 0; i < sourceList.Count; i++){
                if (sourceList[i].Contains(from)){
                    sourceList[i] = sourceList[i].Replace(from, to);
                }
            }
            return sourceList.ToArray();
        }

        //<% Go( method, "hey") %>
        //Container.DataItem, "hedgeReportingSpecifications/hedgeReportingSpecification/hedgeTypeSpecifications/hedgeTypeSpecification/@regressionSignificanceLevel", "#,##0.00 %"
        public static string getMatch(string source, Match match){
            string matchFound = string.Empty;

            matchFound = GetBetweenExcludeTokens(source, match.Start, match.End, 0);
            for (int k = 0; k < match.EmbeddedMatches.Count; k++){
                matchFound = GetBetweenExcludeTokens(
                    matchFound, match.EmbeddedMatches[k].Start, match.EmbeddedMatches[k].End, 0);
            }

            return matchFound;
        }

        public static string[] GetAllBetween(string source, string start, string endMatch, ref int end){
            List<String> results = new List<string>();
            while (true){
                string result = GetBetweenExcludeTokens(source, start, endMatch, end, out end);
                if (result.Length == 0){
                    break;
                }
                results.Add(result);
            }

            return results.ToArray();
        }

        /// <summary>
        ///     Trims a list of strings
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string[] Trim(string[] list){
            for (int i = 0; i < list.Length; i++){
                list[i] = list[i].Trim();
            }
            return list;
        }

        /// <summary>
        ///     See GetBetween. Does not return strStart and strEnd tokens
        ///     Can be used to parse XML in limited cases
        /// </summary>
        /// <param name="strSource">source to look in</param>
        /// <param name="strStart">beginning token</param>
        /// <param name="strEnd">end token </param>
        /// <param name="startMatch">what index to start looking at</param>
        /// <returns>String.Empty on failure, matching string on success</returns>
        public static string GetBetweenExcludeTokens(string strSource, string strStart, string strEnd, int startMatch){
            if (strSource.Contains(strStart) && strSource.Contains(strEnd)){
                int Start = strSource.IndexOf(strStart, startMatch) + strStart.Length;
                int End = strSource.IndexOf(strEnd, Start + 1);
                return strSource.Substring(Start, End - Start);
            } else{
                return String.Empty;
            }
        }

        public static string GetBetweenExcludeTokens(
            string strSource, string strStart, string strEnd, int startMatch, out int End){
            if (strSource.Contains(strStart) && strSource.Contains(strEnd)){
                try{
                    int Start = strSource.IndexOf(strStart, startMatch);
                    if (Start == -1){
                        End = -1;
                        return String.Empty;
                    } else{
                        Start += strStart.Length;
                    }
                    End = strSource.IndexOf(strEnd, Start + 1);
                    string result = strSource.Substring(Start, End - Start);
                    End += strEnd.Length;
                    return result;
                }
                catch{
                    End = -1;
                    return String.Empty;
                }
            } else{
                End = -1;
                return String.Empty;
            }
        }

        #region Nested type: Match

        public class Match{
            private List<Match> embeddedMatches = new List<Match>();
            public string Start { get; set; }
            public string End { get; set; }

            public List<Match> EmbeddedMatches{
                get{
                    return this.embeddedMatches;
                }
                set{
                    this.embeddedMatches = value;
                }
            }
        }

        #endregion
    }
}